package com.ticket.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ticket.entity.ForumPost;
import com.ticket.entity.ForumComment;
import com.ticket.entity.User;
import com.ticket.mapper.ForumPostMapper;
import com.ticket.mapper.ForumCommentMapper;
import com.ticket.service.IForumService;
import com.ticket.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ForumServiceImpl extends ServiceImpl<ForumPostMapper, ForumPost> implements IForumService {

    @Autowired
    private ForumPostMapper postMapper;

    @Autowired
    private ForumCommentMapper commentMapper;

    @Autowired
    private IUserService userService;

    @Override
    public Page<ForumPost> getPostPage(Integer page, Integer size) {
        Page<ForumPost> pageParam = new Page<>(page, size);
        Page<ForumPost> postPage = postMapper.selectPage(pageParam,
            new LambdaQueryWrapper<ForumPost>()
                .orderByDesc(ForumPost::getCreatedAt));

        // 填充用户信息和评论数
        postPage.getRecords().forEach(post -> {
            User user = userService.getById(post.getUserId());
            post.setUsername(user.getUsername());
            post.setUserAvatar(user.getAvatarUrl());

            Long commentCount = commentMapper.selectCount(
                new LambdaQueryWrapper<ForumComment>()
                    .eq(ForumComment::getPostId, post.getId()));
            post.setCommentCount(commentCount.intValue());
        });

        return postPage;
    }

    @Override
    public void createPost(ForumPost post) {
        postMapper.insert(post);
    }

    @Override
    public ForumPost getPostDetail(Long postId) {
        ForumPost post = postMapper.selectById(postId);
        User user = userService.getById(post.getUserId());
        post.setUsername(user.getUsername());
        post.setUserAvatar(user.getAvatarUrl());
        return post;
    }

    @Override
    public Page<ForumComment> getCommentPage(Long postId, Integer page, Integer size) {
        Page<ForumComment> pageParam = new Page<>(page, size);
        Page<ForumComment> commentPage = commentMapper.selectPage(pageParam,
            new LambdaQueryWrapper<ForumComment>()
                .eq(ForumComment::getPostId, postId)
                .orderByAsc(ForumComment::getCreatedAt));

        // 填充用户信息
        commentPage.getRecords().forEach(comment -> {
            User user = userService.getById(comment.getUserId());
            comment.setUsername(user.getUsername());
            comment.setUserAvatar(user.getAvatarUrl());
        });

        return commentPage;
    }

    @Override
    public void createComment(ForumComment comment) {
        commentMapper.insert(comment);
    }

    @Override
    public List<ForumPost> getRecentPosts(int limit) {
        LambdaQueryWrapper<ForumPost> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(ForumPost::getCreatedAt)
                   .last("LIMIT " + limit);
        
        List<ForumPost> posts = postMapper.selectList(queryWrapper);
        
        // 填充用户信息
        posts.forEach(post -> {
            User user = userService.getById(post.getUserId());
            post.setUsername(user.getUsername());
            post.setUserAvatar(user.getAvatarUrl());
        });
        
        return posts;
    }
}
